﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AI3SaveEditor.AI3Files;
using System.IO;

namespace AI3SaveEditor
{
    public partial class ClothesSettingEditor : Form
    {
        Js3cfiFile setting;
        GirlProfile girl;
        ListView clothes;
        private ClothesSettingEditor()
        {
            InitializeComponent();
        }

        public ClothesSettingEditor(GirlProfile girl, ListView clothes)
            : this()
        {
            this.clothes = clothes;
            this.girl = girl;
            string fileName = Path.GetFileNameWithoutExtension(girl.FileName);
            string settingFile = PathHelper.Combine(MainForm.saveFolder, "m_cha", fileName, fileName + ".js3cfi");
            setting = new Js3cfiFile(settingFile);

            foreach (var each in setting.ClothesSettings)
            {
                settings.Items.Add(each);
            }

            this.panel1.Controls.Add(clothes);
            //txtNumber.Maximum = 999 - CollectionHelper.Count(clothesFiles);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //string template = txtPrefix.Text + "{0}";

            //int i = 0;

            //foreach (var each in clothesFiles)
            //{
            //    each.Name = string.Format(template, GetNextNumberString(i + (int)txtNumber.Value));
            //    each.Save();
            //    i++;
            //}
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if (clothes.SelectedItems.Count > 0 && settings.SelectedIndex > -1)
            {
                UpdateItem(settings.SelectedIndex);
            }
        }

        private void UpdateItem(int index)
        {
            ClothesSetting cs = settings.Items[index] as ClothesSetting;
            ClothesFile file = clothes.SelectedItems[0].Tag as ClothesFile;
            cs.File = Path.GetFileNameWithoutExtension(file.FileName);
            cs.Name = file.Name;
            settings.Items[index] = cs;
            Refresh();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            setting.Save();
        }

        private void btnSetAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < settings.Items.Count; i++) {
                UpdateItem(i);
            }
        }
    }
}
